﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Definitions;

namespace Eneta.Portal.Common.Repository
{
    public class MagazineRepository : BaseRepository
    {
        private const string PagesListUrl = "/oppimine/ajakirjad/Lehed/";
        private const string MagazineWebUrl = "/oppimine/ajakirjad/";

        public MagazineRepository(SPWeb web)
            : base(web)
        {

        }

        public bool MagazineExists(string bookName)
        {
            var exists = false;

            var sec = new SPSecurity.CodeToRunElevated(delegate
            {
                using (var site = new SPSite(SiteId))
                using (var web = site.OpenWeb(MagazineWebUrl))
                {
                    var list = web.GetList(PagesListUrl);
                    var queryString = 
                            string.Format(
                                "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>",
                                bookName
                            );
                    var items = ExecuteQuery(list, queryString);
                    if (items == null)
                        exists = false;

                    if (items.Count > 0)
                        exists = true;                    
                }
            });
            SPSecurity.RunWithElevatedPrivileges(sec);

            return exists;
        }

        public PageLayout GetMagazineLayout(PublishingWeb publishingWeb)
        {
            foreach (var layout in publishingWeb.GetAvailablePageLayouts())
            {
                if (layout.Name.ToLower() == "eneta.learning.magazinedetail.layout.aspx")
                {
                    return layout;
                }
            }

            return null;
        }

        public IList<LiteratureDto> GetUserMagazines(int userId)
        {
            var results = new List<LiteratureDto>();

            var queryString = "<Where><Eq><FieldRef Name='RealAuthor' LookupId='True' /><Value Type='User'>{0}</Value></Eq></Where>";
            queryString += "<Eq><FieldRef Name='ContentType' /><Value Type='Text'>{1}</Value></Eq></And></Where>";
            queryString = string.Format(queryString, userId, "Ajakiri");

            var sec = new SPSecurity.CodeToRunElevated(delegate
            {
                using (var site = new SPSite(SiteId))
                using (var web = site.OpenWeb(Webs.Magazines))
                {
                    var list = web.Lists[Lists.Pages];
                    var items = ExecuteQuery(list, queryString);

                    foreach (SPListItem item in items)
                    {
                        results.Add(new LiteratureDto(web, item));
                    }
                }
            });
            SPSecurity.RunWithElevatedPrivileges(sec);

            return results;
        }
    }
}
